package com.example.black.myapplication.androidframwork;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2018/12/24/16:28
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class MyViewPager extends ViewGroup {
    private int mTouchSlop;
    private int leftBound;
    private int rightBound;
    private float downX;
    private float lastMoveX;
    private float moveX;

    public MyViewPager(Context context) {
        super(context);
        initMyViewPager();
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMyViewPager();
    }
    private void initMyViewPager() {
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledPagingTouchSlop();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = getChildCount();
        for (int i = 0;i < size; ++i){//++i? 和i++？？？
            View child = getChildAt(i);
            measureChild(child,widthMeasureSpec,heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed){
            int size = getChildCount();
            for (int i = 0;i< size;++i){
                View child = getChildAt(i);
                child.layout(i * child.getMeasuredWidth(),0,(i+1)*child.getMeasuredWidth(),child.getMeasuredHeight());
                child.setClickable(true);
            }
        }
        leftBound = getChildAt(0).getLeft();
        rightBound = getChildAt(getChildCount()-2).getRight();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = ev.getRawX();
                lastMoveX = downX;
                requestParentDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = ev.getRawX();
                float xDiff = Math.abs(moveX - downX);
                lastMoveX = moveX;
                if (xDiff > mTouchSlop){
                    return true;
                }
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    private void requestParentDisallowInterceptTouchEvent(boolean disallowIntercept) {
        final ViewParent parent = getParent();
        if (parent != null){
            parent.requestDisallowInterceptTouchEvent(disallowIntercept);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE :
                moveX = event.getRawX();
                int scrollDx = (int) (lastMoveX - moveX);
                lastMoveX = moveX;
                if (getScrollX() + scrollDx < leftBound){
                    scrollTo(leftBound,0);
                    return super.onTouchEvent(event);
                }else if (scrollDx>0 && getScrollX() + scrollDx +getWidth() > rightBound){
                    scrollTo(rightBound,0);
                    return super.onTouchEvent(event);
                }
                scrollBy(scrollDx,0);
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return super.onTouchEvent(event);
    }
}
